/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.entities.PriceSlaughterLamb;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import times.persistence.controllers.exceptions.IllegalOrphanException;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.Foreas;

/**
 *
 * @author Administrator
 */
public class ForeasJpaController implements Serializable {

    public ForeasJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Foreas foreas) {
        if (foreas.getPriceSlaughterLambList() == null) {
            foreas.setPriceSlaughterLambList(new ArrayList<PriceSlaughterLamb>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<PriceSlaughterLamb> attachedPriceSlaughterLambList = new ArrayList<PriceSlaughterLamb>();
            for (PriceSlaughterLamb priceSlaughterLambListPriceSlaughterLambToAttach : foreas.getPriceSlaughterLambList()) {
                priceSlaughterLambListPriceSlaughterLambToAttach = em.getReference(priceSlaughterLambListPriceSlaughterLambToAttach.getClass(), priceSlaughterLambListPriceSlaughterLambToAttach.getId());
                attachedPriceSlaughterLambList.add(priceSlaughterLambListPriceSlaughterLambToAttach);
            }
            foreas.setPriceSlaughterLambList(attachedPriceSlaughterLambList);
            em.persist(foreas);
            for (PriceSlaughterLamb priceSlaughterLambListPriceSlaughterLamb : foreas.getPriceSlaughterLambList()) {
                Foreas oldForeasIdOfPriceSlaughterLambListPriceSlaughterLamb = priceSlaughterLambListPriceSlaughterLamb.getForeasId();
                priceSlaughterLambListPriceSlaughterLamb.setForeasId(foreas);
                priceSlaughterLambListPriceSlaughterLamb = em.merge(priceSlaughterLambListPriceSlaughterLamb);
                if (oldForeasIdOfPriceSlaughterLambListPriceSlaughterLamb != null) {
                    oldForeasIdOfPriceSlaughterLambListPriceSlaughterLamb.getPriceSlaughterLambList().remove(priceSlaughterLambListPriceSlaughterLamb);
                    oldForeasIdOfPriceSlaughterLambListPriceSlaughterLamb = em.merge(oldForeasIdOfPriceSlaughterLambListPriceSlaughterLamb);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Foreas foreas) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Foreas persistentForeas = em.find(Foreas.class, foreas.getId());
            List<PriceSlaughterLamb> priceSlaughterLambListOld = persistentForeas.getPriceSlaughterLambList();
            List<PriceSlaughterLamb> priceSlaughterLambListNew = foreas.getPriceSlaughterLambList();
            List<String> illegalOrphanMessages = null;
            for (PriceSlaughterLamb priceSlaughterLambListOldPriceSlaughterLamb : priceSlaughterLambListOld) {
                if (!priceSlaughterLambListNew.contains(priceSlaughterLambListOldPriceSlaughterLamb)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PriceSlaughterLamb " + priceSlaughterLambListOldPriceSlaughterLamb + " since its foreasId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<PriceSlaughterLamb> attachedPriceSlaughterLambListNew = new ArrayList<PriceSlaughterLamb>();
            for (PriceSlaughterLamb priceSlaughterLambListNewPriceSlaughterLambToAttach : priceSlaughterLambListNew) {
                priceSlaughterLambListNewPriceSlaughterLambToAttach = em.getReference(priceSlaughterLambListNewPriceSlaughterLambToAttach.getClass(), priceSlaughterLambListNewPriceSlaughterLambToAttach.getId());
                attachedPriceSlaughterLambListNew.add(priceSlaughterLambListNewPriceSlaughterLambToAttach);
            }
            priceSlaughterLambListNew = attachedPriceSlaughterLambListNew;
            foreas.setPriceSlaughterLambList(priceSlaughterLambListNew);
            foreas = em.merge(foreas);
            for (PriceSlaughterLamb priceSlaughterLambListNewPriceSlaughterLamb : priceSlaughterLambListNew) {
                if (!priceSlaughterLambListOld.contains(priceSlaughterLambListNewPriceSlaughterLamb)) {
                    Foreas oldForeasIdOfPriceSlaughterLambListNewPriceSlaughterLamb = priceSlaughterLambListNewPriceSlaughterLamb.getForeasId();
                    priceSlaughterLambListNewPriceSlaughterLamb.setForeasId(foreas);
                    priceSlaughterLambListNewPriceSlaughterLamb = em.merge(priceSlaughterLambListNewPriceSlaughterLamb);
                    if (oldForeasIdOfPriceSlaughterLambListNewPriceSlaughterLamb != null && !oldForeasIdOfPriceSlaughterLambListNewPriceSlaughterLamb.equals(foreas)) {
                        oldForeasIdOfPriceSlaughterLambListNewPriceSlaughterLamb.getPriceSlaughterLambList().remove(priceSlaughterLambListNewPriceSlaughterLamb);
                        oldForeasIdOfPriceSlaughterLambListNewPriceSlaughterLamb = em.merge(oldForeasIdOfPriceSlaughterLambListNewPriceSlaughterLamb);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = foreas.getId();
                if (findForeas(id) == null) {
                    throw new NonexistentEntityException("The foreas with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Foreas foreas;
            try {
                foreas = em.getReference(Foreas.class, id);
                foreas.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The foreas with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<PriceSlaughterLamb> priceSlaughterLambListOrphanCheck = foreas.getPriceSlaughterLambList();
            for (PriceSlaughterLamb priceSlaughterLambListOrphanCheckPriceSlaughterLamb : priceSlaughterLambListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Foreas (" + foreas + ") cannot be destroyed since the PriceSlaughterLamb " + priceSlaughterLambListOrphanCheckPriceSlaughterLamb + " in its priceSlaughterLambList field has a non-nullable foreasId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(foreas);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Foreas> findForeasEntities() {
        return findForeasEntities(true, -1, -1);
    }

    public List<Foreas> findForeasEntities(int maxResults, int firstResult) {
        return findForeasEntities(false, maxResults, firstResult);
    }

    private List<Foreas> findForeasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Foreas.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Foreas findForeas(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Foreas.class, id);
        } finally {
            em.close();
        }
    }

    public int getForeasCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Foreas> rt = cq.from(Foreas.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
